home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / XPK / Source / shell / xUp.c < prev   
Encoding:
C/C++ Source or Header  |  1997-02-15  |  3.6 KB  |  164 lines

  1. #define NAME     "xUp"
  2. #define REVISION "3"
  3.  
  4. /* Programmheader
  5.  
  6.     Name:        xUp
  7.     Author:        SDI (before 1.1 Urban Dominik Müller)
  8.     Distribution:    PD
  9.     Description:    General XPK file-to-file unpacker
  10.     Compileropts:    -
  11.     Linkeropts:    -l xpkmaster
  12.  
  13.  1.1   09.10.96 : fixed error with new 3.10 xpkmaster.library (A4 problem)
  14.  1.2   29.11.96 : recompiled
  15.  1.3   14.02.96 : corrected suffix removement
  16. */
  17.  
  18. #include "SDI_defines.h"
  19. #define SDI_TO_ANSI
  20. #include "SDI_ASM_STD_protos.h"
  21. #include <pragma/exec_lib.h>
  22. #include <pragma/dos_lib.h>
  23. #include <pragma/xpkmaster_lib.h>
  24.  
  25. #ifdef __MAXON__
  26.   #define __asm
  27.   #define __saveds
  28. #endif
  29.  
  30. struct Library *XpkBase = 0;
  31.  
  32. UBYTE errbuf[XPKERRMSGSIZE + 1];    /* +1 to make room for '\n'*/
  33.  
  34. ULONG __asm __saveds chunkfunc(register __a1 struct XpkProgress *prog)
  35. {
  36.   if(prog->xp_Type == XPKPROG_START)
  37.     printf("\033[0 p");
  38.  
  39.   if(prog->xp_Type != XPKPROG_END)
  40.     printf("\r%4s: %-8s (%3ld%% done, %2ld%% CF, %6ld cps) %s\033[K",
  41.        prog->xp_PackerName, prog->xp_Activity, prog->xp_Done,
  42.        prog->xp_CF, prog->xp_Speed, prog->xp_FileName);
  43.   else
  44.     printf("\r%4s: %-8s (%3ldK, %2ld%% CF, %6ld cps) %s\033[K\n",
  45.        prog->xp_PackerName, prog->xp_Activity, prog->xp_ULen >> 10,
  46.        prog->xp_CF, prog->xp_Speed, prog->xp_FileName);
  47.  
  48.   if(prog->xp_Type == XPKPROG_END)
  49.     printf("\033[1 p");
  50.  
  51.   return SetSignal(0, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C;
  52. }
  53.  
  54. struct Hook chunkhook = {{0}, (ULONG (*) ()) chunkfunc};
  55.  
  56. UBYTE namebuf[200];
  57.  
  58. STRPTR tempname(STRPTR name)
  59. {
  60.   ULONG i = strlen(name);
  61.   CopyMem(name, namebuf, i);
  62.   for(name = namebuf + i; name > namebuf; name--)
  63.     if(name[-1] == '/' || name[-1] == ':')
  64.       break;
  65.  
  66.   sprintf(name, "tmp%lx", &name);
  67.   return namebuf;
  68. }
  69.  
  70. void end(STRPTR text)
  71. {
  72.   if(text)    printf(text);
  73.   if(XpkBase)    CloseLibrary(XpkBase);
  74.   exit(text ? RETURN_ERROR : 0);
  75. }
  76.  
  77. void main(int argc, char **argv)
  78. {
  79.   LONG res, i, suffix, len, sufmode = 0;
  80.   STRPTR password = 0;
  81.  
  82.   if(!(XpkBase = OpenLibrary(XPKNAME, 0)))
  83.     end("Cannot open " XPKNAME "\n");
  84.  
  85.   if(argc == 1 || !strcmp (argv[1], "?"))
  86.     end("Usage: " NAME " [-s|-S] [-p password] files\n");
  87.  
  88.   for(i = 1; i + 1 < argc; ++i) // find params
  89.   {
  90.     if(!strcmp(argv[i], "-s"))
  91.       sufmode = 1;
  92.     else if(!strcmp(argv[i], "-S"))
  93.       sufmode = 2;
  94.     else if(!strcmp(argv[i], "-p"))
  95.       password = argv[++i];
  96.     else
  97.       break;
  98.   }
  99.  
  100.   for(; i < argc; i++)
  101.   {
  102.     tempname(argv[i]);
  103.     len = strlen(argv[i]);
  104.     suffix = 0;
  105.     if(sufmode == 1 && len > 4 && !stricmp(argv[i] + len - 4, ".xpk"))
  106.     {
  107.       CopyMem(argv[i], namebuf, len-4);
  108.       namebuf[len-4] = 0;
  109.       suffix = 1;
  110.     }
  111.     else if(sufmode == 2 && len > 2)
  112.     {
  113.       char *end = argv[i] + len-1;
  114.  
  115.       while(end > argv[i])
  116.       {
  117.         if(*end == '.')
  118.         {
  119.       CopyMem(argv[i], namebuf, end-argv[i]);
  120.       namebuf[end-argv[i]] = 0;
  121.           suffix = 1; break;
  122.         }
  123.         --end;
  124.       }
  125.     }
  126.  
  127.     if((res = XpkUnpackTags(
  128.     XPK_InName, (ULONG) argv[i],
  129.     XPK_FileName, (suffix ? namebuf : argv[i]),
  130.     XPK_OutName, (ULONG) namebuf,
  131.     XPK_ChunkHook, (ULONG) &chunkhook,
  132.     XPK_Password, (ULONG) password,
  133.     XPK_GetError, (ULONG) errbuf,
  134.     XPK_NoClobber, TRUE,
  135.     TAG_DONE
  136.     )) && res != XPKERR_NOTPACKED)
  137.     {
  138.       if(errbuf)
  139.       {
  140.         ULONG i = strlen(errbuf);
  141.         errbuf[i++] = '\n';
  142.         errbuf[i] = 0;
  143.       }
  144.       end(errbuf);
  145.     }
  146.  
  147.     if(res)
  148.     {
  149.       printf("%s\n", errbuf);
  150.       if(!DeleteFile(namebuf))
  151.     end("Cannot delete outfile\n");
  152.     }
  153.  
  154.     else if(!suffix)
  155.     {
  156.       if(!DeleteFile(argv[i]))
  157.     end("Cannot delete input file\n");
  158.       if(!Rename(namebuf, argv[i]))
  159.     end("Cannot rename tempfile\n");
  160.     }
  161.   }
  162.   end(0);
  163. }
  164.